package live.jipiao.dao;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import live.common.AirParam;
import live.common.ErrorException;
import live.common.Verifystring;
import live.jipiao.entity.AirQuery;
import live.jipiao.entity.PnrInfo;
import live.jipiao.entity.AirData.FlightItem;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;


public class CreatePnr {
	private XmlFormart xfm = null;
	private FlightCommon fc = null;
	private Verifystring verify = null;
	private Document doc = null;
	private String postResult=null,result=null;
	private PnrInfo pnrInfo;
	private Element e = null;
	
	
	/**
	 * 创建PNR
	 * @param item
	 * @return
	 * @throws DocumentException 
	 */
	public PnrInfo createPnr(AirQuery query,String passangerInfo,List<FlightItem> chooseOrders) throws ErrorException, DocumentException{
		postResult = returnResponse(query,passangerInfo,chooseOrders);
		if (postResult != null) {
			xfm = new XmlFormart();
			fc = new FlightCommon();
			verify = new Verifystring();
			try {
				pnrInfo = new PnrInfo();
				doc = xfm.xmlToDocument(postResult);
				System.out.println(doc.asXML());
				if (xfm.returnSingleNode(doc, "//is_success").equals("T")) {
					pnrInfo = returnNodeValue(doc);
					pnrInfo.setPnrInfo(fc.returnPnrInfo(pnrInfo));
					pnrInfo.setPassengerInfo(passangerInfo);
					pnrInfo.setIs_success(true);
				} else {
					result = xfm.returnSingleNode(doc, "//error");
					pnrInfo.setError(result);
					pnrInfo.setIs_success(false);
				}
			} catch (DocumentException e) {
				e.printStackTrace();
				throw new ErrorException(new DocumentException("Create成人PNR信息XML转换Document换出现异常"+e.getMessage()));
			}finally{
				xfm = null;
				fc = null;
				verify = null;
			}
		}else{
			throw new ErrorException("Create成人PNR信息时接口返回对象NULL");
		}
		return pnrInfo;
	}
	
	/**
	 * 封装PNR数据信息
	 * @param doc
	 * @param item
	 * @return
	 */
	private PnrInfo returnNodeValue(Document doc) {
		Iterator iter = doc.selectNodes("//PnrInfo").iterator();
		while (iter.hasNext()) {
			e = (Element) iter.next();
			pnrInfo = new PnrInfo();
			pnrInfo.setCode(e.elementTextTrim("Code"));
			pnrInfo.setBigCode(e.elementTextTrim("BigCode"));
			pnrInfo.setTeamType(e.elementTextTrim("TeamType"));
			pnrInfo.setRouteType(e.elementTextTrim("RouteType"));
			pnrInfo.setFare(e.elementTextTrim("Fare"));
			pnrInfo.setTax(e.elementTextTrim("Tax"));
			pnrInfo.setYq(e.elementTextTrim("YQ"));
			pnrInfo.setLowFare(e.elementTextTrim("LowFare"));
			pnrInfo.setOfficeNo(e.elementTextTrim("OfficeNo"));
		}
		return pnrInfo;
	}
	
	/**
	 * PNR接口返回XML信息
	 * @param item
	 * @return
	 */
	private String returnResponse(AirQuery item,String passangerInfo,List<FlightItem> chooseOrders) throws ErrorException {
		xfm = new XmlFormart();
		fc = new FlightCommon();
		verify = new Verifystring();
		Map<String, String> params = xfm
				.returnPublicPostData(AirParam.C_PNR.toString());
		String flightInfo = fc.returnFlightInfoForPnr(item, chooseOrders);
		params.put("flightInfo", flightInfo);
		params.put("passengerInfo", passangerInfo);
		String sign = verify.returnAirMd5Hex(params);
		params.put("sign", sign);
		try {
			postResult = WebServicesSub.getInstance().returnResult(params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			log.error("获取成人PNR返回信息出现异常"+e.getMessage());
			postResult = null;
			throw new ErrorException("获取成人PNR返回信息出现异常"+e.getMessage());
		}
		finally{
			xfm = null;
			fc = null;
			verify = null;
		}
		return postResult;
	}
}
